package cn.mesmile.workflow.util;

import org.flowable.bpmn.converter.BpmnXMLConverter;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.bpmn.model.ExtensionAttribute;
import org.flowable.common.engine.impl.util.io.StringStreamSource;

import java.util.List;

/**
 * @author zb
 * @Description
 */
public class WorkFlowUtil {

    private WorkFlowUtil(){}

    /**
     * TODO 实现自己的逻辑，获取当前用户
     * @return 当前操作用户的id
     */
    public static String getCurrentUserId(){
        return "admin";
    }

    /**
     * TODO 实现自己的逻辑，获取当前用户
     * @return 当前操作用户的名称
     */
    public static String getCurrentUserName(){
        return "管理员";
    }

    /**
     *  字符串xml转bpmnModel对象
     * @param xml xml
     * @return BpmnModel对象
     */
    public static BpmnModel getBpmnModel(String xml) {
        BpmnXMLConverter converter = new BpmnXMLConverter();
        return converter.convertToBpmnModel(new StringStreamSource(xml), false, false);
    }


    /**
     * 获取根节点扩展属性value
     * @param model BpmnModel对象
     * @param key   扩展元素key
     */
    public static String getProcessExtensionAttribute(BpmnModel model, String key) {
        List<ExtensionAttribute> attributes = model.getMainProcess().getAttributes().get(key);
        if (attributes != null && attributes.size() > 0) {
            return attributes.get(0).getValue();
        }
        return null;
    }

}
